package com.github.livebase.log;

import androidx.annotation.NonNull;

import com.github.livebase.util.base.CollectionsUtil;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName LoggerConfig
 * @description:
 * @author: biao532
 * @create: 2024-02-02 21:14
 * @Version 1.0
 **/
public class LoggerDefinition {
    public class Parameter {
        String parameter;
        Object value;
        Class clazz;

        public Parameter(String parameter, Object value, Class clazz) {
            this.parameter = parameter;
            this.value = value;
            this.clazz = clazz;
        }

        @Override
        public String toString() {
            return "Parameter{" +
                    "parameter='" + parameter + '\'' +
                    ", value='" + value + '\'' +
                    ", clazz='" + clazz + '\'' +
                    '}';
        }
    }
    private String packageName;
    private String className;
    private List<Parameter> params;
    public String getClassName() {
        return className;
    }

    public LoggerDefinition(@NonNull String className, @NonNull String packageName) {
        this.packageName = packageName;
        this.className = className;
        this.params = new ArrayList<>();
    }
    public String getPackageName() {
        return packageName;
    }

    public void addParameter(String param, Object value, Class clazz) {
        Parameter p = filter(param);
        if (p == null) {
            params.add(new Parameter(param, value, clazz));
            return;
        }
        p.value = value;
        p.clazz = clazz;
    }

    public Object getParamValue(String param) {
        Parameter p = filter(param);
        if (null == p) {
            return null;
        }
        return p.value;
    }
    public List<Parameter> getParams() {
        return params;
    }

    private Parameter filter(String param) {
        return CollectionsUtil.first(params, parameter -> param.equals(parameter.parameter) );
    }
    @Override
    public String toString() {
        return "LoggerDefinition{" +
                "packageName='" + packageName + '\'' +
                ", className='" + className + '\'' +
                ", params=" + params +
                '}';
    }
}
